package light.head.controller.oa;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.oa.AnnouncementDAO;
import light.head.dao.oa.RewardDAO;
import light.head.dao.sys.MessageDAO;
import light.head.dao.sys.MessageTemplateDAO;
import light.head.dao.sys.UserInfoDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.Announcement;
import light.head.model.Reward;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


@Controller
@RequestMapping(Url.C_REWARD)
public class RewardController extends SupporterController{
	
	private RewardDAO rewardDAO;
	private AnnouncementDAO annDAO;
	private UserInfoDAO userDAO;
	private MessageDAO msgDAO;
	private MessageTemplateDAO mtDAO;
	
	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.REWARD_INIT);
	}
	
	@RequestMapping(value = Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.UN, Parm.RTYPE, Parm.OC);
		map.put(Parm.OC, cu_organCode(request).toString());
		if (rewardDAO.findByMap(map, pb) == RESULT.SUCCESS)
			request.setAttribute(Parm.PAGE_BEAN, pb);
		
		return new ModelAndView(Url.REWARD_LIST);
	}
	
	@RequestMapping(value = Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		if(null == cu_organCode(request)){
			PageBean pb = createPageBean(request);
			request.setAttribute(Parm.PAGE_BEAN, pb);
			return new ModelAndView(Url.REWARD_INIT);
		}
		String code = request.getParameter(Parm.CODE);
		if (isNotNull(code)) {
			Reward reward = rewardDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.REWARD, reward);
		}
		return new ModelAndView(Url.REWARD_EDITT);
	}
	
	@RequestMapping(Url.SAVE)
	public void save(Reward reward, HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		if (reward.getUc() != null) {
			reward.setCu(cu_code(request));
			reward.setCt(new Date());
			reward.setDeleted(false);
			reward.setOc(cu_organCode(request));
			result = rewardDAO.add(reward);
		} else {
			print(response, Msg.EXISTS);
			return;
		}
		String name = userDAO.findName(reward.getUc());
		Announcement ann = new Announcement();
		ann.setCt(reward.getCt());
		ann.setCu(reward.getCu());
		ann.setDeleted(false);
		ann.setOc(reward.getOc());
		ann.setDraft(false);
		ann.setRt(new Date());
		if(reward.getRtype() == 1){
		ann.setTitle("奖励" + name);
		ann.setContent(name +  "得到奖励，原因是：" +reward.getReason() + "，奖励内容：" + reward.getContent() + "，希望再接再厉！");
		}else{
		ann.setTitle("惩罚" + name);
		ann.setContent(name + "受到惩罚，原因是：" +reward.getReason() + "，惩罚内容：" + reward.getContent() + "，希望以此为戒！");
		}
		annDAO.add(ann);
		if (RESULT.SUCCESS == result) {
			//消息通知审核人
			Integer ac =userDAO.findCode("彭先辉");
			String content = mtDAO.applyTofirst(ann,cu_realName(request));
			msgDAO.sendToUsers("【" + cu_realName(request) + "】提交公告申请单待审核通知", content, cu_code(request), cu_organCode(request),ac);
			print(response, Msg.SUCCESS);
		} else
			print(response, Msg.ERROR);
	}
	
	@RequestMapping(value = Url.DELETE)
	public void delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		
		if (rewardDAO.del(Integer.parseInt(code)) == RESULT.SUCCESS)
			print(response, Msg.SUCCESS);
		else 
			print(response, Msg.ERROR);
	}
	@RequestMapping(value = Url.SHIELD)
	public void shield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		Reward g = rewardDAO.get(Integer.parseInt(code));
		if (null == g) {
			print(response, Msg.NOT_FIND);
			return;
		}
		g.setDeleted(true);
		if (rewardDAO.upd(g) == RESULT.SUCCESS)
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}
	
	@RequestMapping(value = Url.UN_SHIELD)
	public void unshield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		Reward g = rewardDAO.get(Integer.parseInt(code));
		if (null == g) {
			print(response, Msg.NOT_FIND);
			return;
		}
		g.setDeleted(false);
		if (rewardDAO.upd(g) == RESULT.SUCCESS)
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}
	
	@Autowired
	public void setRewardDAO(RewardDAO rewardDAO) {
		this.rewardDAO = rewardDAO;
	}
	@Autowired
	public void setAnnDAO(AnnouncementDAO annDAO) {
		this.annDAO = annDAO;
	}
	@Autowired
	public void setUserDAO(UserInfoDAO userDAO) {
		this.userDAO = userDAO;
	}
	@Autowired
	public void setMsgDAO(MessageDAO msgDAO) {
		this.msgDAO = msgDAO;
	}
	@Autowired
	public void setMtDAO(MessageTemplateDAO mtDAO) {
		this.mtDAO = mtDAO;
	}

}
